<template>
	<view class="__pd_ocr">
		<ocr-plugin :show='show' :getAuthorization='getAuthorization' :fixedSecret='true' :showCancel='true'
			@success="successHandler" @error="errorHandler" @fail="failHandler" @cancel="show = false"
			:ocrType="type"></ocr-plugin>
	</view>
</template>

<script setup lang="ts">
import PD from '../libs';
import type { TsHelp } from '../libs/ts-help';


withDefaults(defineProps<{
	type: 'BankCardOCR' | 'IDCardOCR',//ocr识别类型
}>(), {
	type: 'BankCardOCR',
});

const show = ref(false);

let resolver : TsHelp.PromiseResolver<{value : string}>;
let rejecter : () => void;
const open = () : Promise<{value : string}> => {
	return new Promise((r,j) => {
		resolver = r;
		rejecter = j;
		show.value = true;
	});
};

const close = () => {
	rejecter && rejecter();
	show.value = false;
};

const getAuthorization = () => {
	return {
		secretId: 'AKID3vZEoHmCZ1Qj3Ohpvw0yuiQwOqBKTYem',
		secretKey: '780iCTecRBpSzVuDRXYP7oRzTuLgtxsp'
	};
};

const successHandler = (e : {detail : {result : {CardNo : string}}}) => {
	resolver && resolver({
		value : e.detail.result.CardNo
	});
	close();
};

const errorHandler = () => {
	PD.UniHelp.modal('图片识别失败，请重新操作或手动输入');
	close();
};

const failHandler = () => {
	PD.UniHelp.modal('图片识别失败，请重新操作或手动输入');
	close();
};


defineExpose({
	open,
});

</script>

<style scoped lang="less"></style>